package hust.ioic.oa.view.listener;

import hust.ioic.oa.domain.Department;
import hust.ioic.oa.domain.MessageQuene;
import hust.ioic.oa.domain.Operator;
import hust.ioic.oa.domain.Rdfile;
import hust.ioic.oa.service.MessageQueneService;
import hust.ioic.oa.service.OperatorService;
import hust.ioic.oa.service.RdfileService;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.google.gson.Gson;

public class NumServlet extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		WebApplicationContext ac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext());
		Gson gson = new Gson();
		String nid = req.getParameter("num");
		int id = Integer.parseInt(nid);
		MessageQueneService mqs = (MessageQueneService) ac
				.getBean("messageQueneServiceImpl");
		List<MessageQuene> mqsList = mqs.getMBySender(id);
		List<MessageQuene> mqrList = mqs.getMByRecevier(id);
		Integer n = mqsList.size() + mqrList.size();
		List<Integer> ln = new ArrayList<>();
		ln.add(n);
		String result = gson.toJson(ln);
		System.out.println(result);

		resp.setContentType("application/json; charset=utf-8");
		resp.setHeader("pragma", "no-cache");
		resp.setHeader("cache-control", "no-cache");
		PrintWriter out = resp.getWriter();
		out.println(result);
		out.flush();
	}

}
